﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DAO;
using BUS;
namespace ProjectM2Work.Pages
{
    public partial class PageTracNghiem : PhoneApplicationPage
    {
        bool isScale = false;
        TuVungBUS tuvung = new TuVungBUS();
        TuDien WordNow = null;
        int _score;
        int _prescore;
        bool isSelect = true;
        public PageTracNghiem()
        {
            InitializeComponent();
            RandomQuestion();
            _score = 0;
            _prescore = 0;
        }

#region  Vung tao hieu ung cho button
        private void btnThoat_MouseLeave(object sender, MouseEventArgs e)
        {
           App.ZoomInImage(sender);
        }

        private void btnThoat_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           App.ZoomOutImage(sender);
        }

        private void btnThoat_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }
#endregion

        private void RandomQuestion()
        {
            txbDapAnA.Foreground = new SolidColorBrush(Colors.Black);
            txbDapAnB.Foreground = new SolidColorBrush(Colors.Black);
            txbDapAnC.Foreground = new SolidColorBrush(Colors.Black);
            txbDapAnD.Foreground = new SolidColorBrush(Colors.Black);
            txbDapAnA.Text = ""; txbDapAnB.Text = ""; txbDapAnC.Text = ""; txbDapAnD.Text = "";
            WordNow = null;
            while (WordNow == null)
            {
                int idQuestion = Everywhere.rand.Next(1, 4400);
                WordNow = tuvung.LayTuVungTheoID(idQuestion.ToString());
            }
            txbNoiDungCauHoi.Text = WordNow.Description.ToString();
            switch (Everywhere.rand.Next(1, 4))
            {
                case 1: txbDapAnA.Text = WordNow.VnWord; break;
                case 2: txbDapAnB.Text = WordNow.VnWord; break;
                case 3: txbDapAnC.Text = WordNow.VnWord; break;
                case 4: txbDapAnD.Text = WordNow.VnWord; break;
            }

            for (int i = 0; i < 4; i++)
            {
                TuDien temp = null;
                if (txbDapAnA.Text == "")
                {
                    bool NoOk = true;
                    while (temp == null || NoOk)
                    {
                        temp = tuvung.LayTuVungTheoID(Everywhere.rand.Next(1, 4400).ToString());
                        NoOk = false;
                        if (temp != null && temp.VnWord == WordNow.VnWord)
                            NoOk = true;
                    }
                    txbDapAnA.Text = temp.VnWord;
                } 
                else if (txbDapAnB.Text == "")
                {
                    bool NoOk = true;
                    while (temp == null || NoOk)
                    {
                        temp = tuvung.LayTuVungTheoID(Everywhere.rand.Next(1, 4400).ToString());
                        NoOk = false;
                        if (temp != null && temp.VnWord == WordNow.VnWord)
                            NoOk = true;
                    }
                    txbDapAnB.Text = temp.VnWord;
                }
                else if (txbDapAnC.Text == "")
                {
                    bool NoOk = true;
                    while (temp == null || NoOk)
                    {
                        temp = tuvung.LayTuVungTheoID(Everywhere.rand.Next(1, 4400).ToString());
                        NoOk = false;
                        if (temp != null && temp.VnWord == WordNow.VnWord)
                            NoOk = true;
                    }
                    txbDapAnC.Text = temp.VnWord;
                }
                else if (txbDapAnD.Text == "")
                {
                    bool NoOk = true;
                    while (temp == null || NoOk)
                    {
                        temp = tuvung.LayTuVungTheoID(Everywhere.rand.Next(1, 4400).ToString());
                        NoOk = false;
                        if (temp != null && temp.VnWord == WordNow.VnWord)
                            NoOk = true;
                    }
                    txbDapAnD.Text = temp.VnWord;
                }
            }
        }

        private void UpdateScore()
        {
            _prescore = _score;
            _score += 10;
            txbDiem.Text = _score.ToString();
        }

        private void ResetScore()
        {
            _prescore = 0;
            _score = 0;
            txbDiem.Text = _score.ToString();
        }
 

   

        private void btnThoat_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }

        private void btnThongTin_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/PageThongTin.xaml", UriKind.Relative));
        }



        private void btnChonA_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSelect)
            {
                isSelect = false;
                if (txbDapAnA.Text == WordNow.VnWord)
                {
                    txbDapAnA.Foreground = new SolidColorBrush(Colors.Blue);
                    UpdateScore();
                }
                else
                {
                    ResetScore();
                    txbDapAnA.Foreground = new SolidColorBrush(Colors.Red);
                    if (txbDapAnB.Text == WordNow.VnWord)
                        txbDapAnB.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnC.Text == WordNow.VnWord)
                        txbDapAnC.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnD.Text == WordNow.VnWord)
                        txbDapAnD.Foreground = new SolidColorBrush(Colors.Blue);
                }
            }
        }

        private void btnChonC_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSelect)
            {
                isSelect = false;
                if (txbDapAnC.Text == WordNow.VnWord)
                {
                    txbDapAnC.Foreground = new SolidColorBrush(Colors.Blue);
                    UpdateScore();
                }
                else
                {
                    ResetScore();
                    txbDapAnC.Foreground = new SolidColorBrush(Colors.Red);
                    if (txbDapAnB.Text == WordNow.VnWord)
                        txbDapAnB.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnA.Text == WordNow.VnWord)
                        txbDapAnA.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnD.Text == WordNow.VnWord)
                        txbDapAnD.Foreground = new SolidColorBrush(Colors.Blue);
                }
            }
        }

        private void btnChonD_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSelect)
            {
                isSelect = false;
                if (txbDapAnD.Text == WordNow.VnWord)
                {
                    txbDapAnD.Foreground = new SolidColorBrush(Colors.Blue);
                    UpdateScore();
                }
                else
                {
                    ResetScore();
                    txbDapAnD.Foreground = new SolidColorBrush(Colors.Red);
                    if (txbDapAnB.Text == WordNow.VnWord)
                        txbDapAnB.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnC.Text == WordNow.VnWord)
                        txbDapAnC.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnA.Text == WordNow.VnWord)
                        txbDapAnA.Foreground = new SolidColorBrush(Colors.Blue);
                }
            }
        }

        private void bntChonB_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isSelect)
            {
                isSelect = false;
                if (txbDapAnB.Text == WordNow.VnWord)
                {
                    txbDapAnB.Foreground = new SolidColorBrush(Colors.Blue);
                    UpdateScore();
                }
                else
                {
                    ResetScore();
                    txbDapAnB.Foreground = new SolidColorBrush(Colors.Red);
                    if (txbDapAnA.Text == WordNow.VnWord)
                        txbDapAnA.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnC.Text == WordNow.VnWord)
                        txbDapAnC.Foreground = new SolidColorBrush(Colors.Blue);
                    else if (txbDapAnD.Text == WordNow.VnWord)
                        txbDapAnD.Foreground = new SolidColorBrush(Colors.Blue);
                }
            }
        }

        private void button1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_prescore != _score || (_prescore == 0 && _score == 0))
            {
                RandomQuestion();
                isSelect = true;
            }
        }
    }
}